# !/usr/bin/env python
# -*-coding:utf-8 -*-

"""
# File       : price.py
# Time       ：2022/9/23 17:54
# Author     ：Ray Zhong
# version    ：python 3.7
# Description：
"""
import images
import wx,time,random,copy,sys,os
import wx.lib.buttons as buttons
from wx.lib.embeddedimage import PyEmbeddedImage
import threading
class xiuli(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, '第一代抽奖系统', size=(550, 350),
                          style=wx.CAPTION | wx.MINIMIZE_BOX | wx.CLOSE_BOX | wx.SYSTEM_MENU)

        image = PyEmbeddedImage(images.head0).GetBitmap()
        icon=wx.Icon()
        icon.CopyFromBitmap(image)
        self.SetIcon(icon)

        self.InitUI()
        self.Centre()
        self.Show()

    def InitUI(self):
        global bitmap0

        panel = wx.Panel(self)
        bitmap0 = wx.StaticBitmap(panel, -1, PyEmbeddedImage(images.backg).GetBitmap(), (0, 0))
        # sizer = wx.GridBagSizer(5,5)

        path = r"name.txt"
        if not os.path.exists(path):
            self.names=['影','真','雷电芽衣','八重樱']
            self.name1 = copy.copy(self.names)
            b = '\n'.join(self.names)

        else:
            f = open(path, 'r', encoding='utf-8')
            b = f.readlines()
            f.close()

            self.names=[i.replace('\n','') for i in b]
            self.name1 = copy.copy(self.names)

            b = ''.join(b)
        input1 = wx.TextCtrl(bitmap0,-1,'候选人名单:\n'+b,size=(-1,300),pos=(5,5),style=wx.TE_READONLY|wx.TE_MULTILINE)
        input1.SetBackgroundColour((255,255,240))

        self.output0 = wx.TextCtrl(bitmap0,-1,'',size=(100,-1),pos = (175,50),style=wx.TE_READONLY)
        self.output0.SetBackgroundColour((211, 211, 211))
        # output0.Disable()
        self.input2 = wx.TextCtrl(bitmap0,-1,'预选人数',size=(60,-1),pos=(300,50))

        self.input3 = wx.TextCtrl(bitmap0,-1,'抽选结果...\n------',size=(150,300),pos=(370,5),style=wx.TE_READONLY|wx.TE_MULTILINE)
        self.input3.SetBackgroundColour((255,228,181))


        self.button1 = buttons.GenBitmapTextButton(bitmap0, -1,PyEmbeddedImage(images.pic1).GetBitmap(), " 复位抽取   ",
                                                   style=wx.IMAGE_ALPHA_THRESHOLD | wx.BORDER, pos=(175, 100),size=(185,35),name = '0')

        self.button2 = buttons.GenBitmapTextButton(bitmap0, -1, PyEmbeddedImage(images.pic2).GetBitmap(), " 清除结果   ",
                                                   style=wx.IMAGE_ALPHA_THRESHOLD | wx.BORDER, pos=(175, 150), size=(185, 35),name='1')
        self.button3 = buttons.GenBitmapTextButton(bitmap0, -1, PyEmbeddedImage(images.pic3).GetBitmap(), " 不复位抽取",
                                                   style=wx.IMAGE_ALPHA_THRESHOLD | wx.BORDER, pos=(175, 200), size=(185, 35),name='2')
        self.button4 = buttons.GenBitmapTextButton(bitmap0, -1, PyEmbeddedImage(images.pic4).GetBitmap(), " 停下来      ",
                                                   style=wx.IMAGE_ALPHA_THRESHOLD | wx.BORDER, pos=(175, 250), size=(185, 35),name='3')


        self.input2.Bind(wx.EVT_LEFT_DOWN, self.text0)
        self.button1.Bind(wx.EVT_BUTTON, self.start1)
        self.button2.Bind(wx.EVT_BUTTON, self.start1)
        self.button3.Bind(wx.EVT_BUTTON, self.start1)
        self.button4.Bind(wx.EVT_BUTTON, self.end1)
        self.button4.Disable()
    def text0(self,event):

        if "预选人数" in self.input2.GetValue():
            self.input2.ChangeValue("")
        self.input2.SetFocus()
        if self.input2.GetValue() != "":
            self.input2.Unbind(wx.EVT_LEFT_DOWN)

    def start1(self,event):
        btn = event.GetEventObject()
        btn.Disable()
        btnname = btn.GetName()

        if btnname == '1':
            self.reclear()
            btn.Enable()
            return

        numb = self.input2.GetValue()
        try:   #检查输入类型
            numb = int(numb)
        except:
            wx.MessageBox('请填入正确人数')
            btn.Enable()
            return

        self.btn = btn
        if btnname=='0':
            self.button3.Disable()
            self.button4.Enable()
            self.numb = numb
            self.name1 = copy.copy(self.names)
            self.refresh_fun()
        elif btnname=='2':
            # self.button1.Disable()
            if numb not in range(1, len(self.name1)+1):
                wx.MessageBox('人数过多')
                btn.Enable()
                return
            self.button4.Enable()
            self.numb = numb
            self.refresh_fun()
        else:
            pass

    def end1(self,event):
        self.testThread.setFlag(False)
        if self.btn.GetName() =='2':
            self.continuego()
            self.button3.Enable()
            self.button1.Enable()
        elif self.btn.GetName()=='0':
            self.justgo()
            self.button3.Enable()
            self.button1.Enable()
        self.button4.Disable()
    def refresh_fun(self):
        self.op = True
        self.testThread = MyThread(self.output0,self.name1)
        self.testThread.setDaemon(True)  # 设为保护线程，主进程结束会关闭线程
        self.testThread.getParm()  # 获得线程内部值
        self.testThread.setParm(1)  # 修改线程内部值
        self.testThread.start()  # 开始线程


    def reclear(self):
        self.input3.SetValue('抽选结果...\n------')


    def continuego(self):
        # print()
        for i in range(self.numb):
            b = random.choice(self.name1)
            self.input3.SetValue(self.input3.GetValue() + '\n' + b)
            self.name1.remove(b)

        self.input3.SetValue(self.input3.GetValue() + '\n' + '------')


    def justgo(self):                #重置抽取

        self.name1 = copy.copy(self.names)
        for i in range(self.numb):

            b = random.choice(self.name1)
            self.input3.SetValue(self.input3.GetValue() + '\n' + b)
            self.name1.remove(b)

        self.input3.SetValue(self.input3.GetValue() + '\n' + '------')
        self.name1 = copy.copy(self.names)
class MyThread(threading.Thread):
    def __init__(self, aaa,names):
        threading.Thread.__init__(self)
        self.Flag = True  # 停止标志位
        self.Parm = 0  # 用来被外部访问的
        # 自行添加参数
        self.aaa = aaa
        self.names=names

    def run(self):
        while (True):
            if (not self.Flag):
                self.aaa.SetLabel('抽选完毕')
                break
            else:
                self.aaa.SetLabel(random.choice(self.names))

                time.sleep(0.1)

    def setFlag(self, parm):  # 外部停止线程的操作函数
        self.Flag = parm  # boolean

    def setParm(self, parm):  # 外部修改内部信息函数
        self.Parm = parm

    def getParm(self):  # 外部获得内部信息函数
        return self.Parm
if __name__ == '__main__':
    app = wx.App()  # 创建应用程序对象
    login = xiuli()
    app.MainLoop()